<?php

include_once('../@base/portal.php');
include_once('../login/login.php');
// include_once('functions');

$filter = $post->filter;
$user = $post->user;
$login = login($user->number, $user->passwd);
if ($login['status'] == 0 && $login['userbase']['level'] > 0) {
    output(doQuery($filter));
} else {
    return [
        "status" => 1
    ];
}
function doQuery($filter)
{
    $sql = "SELECT * FROM oprec_inspect_record";
    if (count($filter) > 0) {
        $sql .= " WHERE ";
        $all = array();
        foreach ($filter as $ragulation) {
            $column = array();
            foreach ($ragulation->values as $values) {
                $pairs = array();
                foreach ($ragulation->keys as $idx => $key) {
                    array_push($pairs, "{$key} {$ragulation->operator} '{$values[$idx]}'");
                }
                array_push($column, join(' AND ', $pairs));
            }
            array_push($all, '(' . join(' OR ', $column) . ')');
        }
        $sql .= join(' AND ', $all);
    }
    $sql .= " ORDER BY CONVERT( clientName USING gbk ) COLLATE gbk_chinese_ci ASC , time ASC , CONVERT( userName USING gbk ) COLLATE gbk_chinese_ci ASC";
    // logfile('QUERY_INSPECT', $sql);
    $result = exec_sql($sql);
    return [
        'status' => 0,
        'content' => $result
    ];
}
